﻿using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;


public partial class Reports_CandidateWIP_ReportViewer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UserID.Text = Convert.ToString(CommonUtility.GetLoggedOnUserId());
            GetRoleDetails();
            GetRegionData();
            GetCountryData();
            if (RoleID.Text =="RD")
            {
                trRegion.Visible = false;
                trCountry.Visible = false;
            }
            else if(RoleID.Text == "IQKSAD" || RoleID.Text == "IQKSMG")
            {
                trRegion.Visible = true;
                trCountry.Visible = true;
            }
            else
            {

                trRegion.Visible = false;
                trCountry.Visible = false;
            }
        }
    }

    private void GetRoleDetails()
    {
        UserMasterBAL objUMBal = new UserMasterBAL();
        DataSet dsUserDetails = new DataSet();
        Guid UserId = Guid.Parse(UserID.Text);
        dsUserDetails = objUMBal.GetUserDetails(UserId);

        if (dsUserDetails != null)
        {
            if (dsUserDetails.Tables.Count > 0)
            {
                if (dsUserDetails.Tables[0].Rows.Count > 0)
                {
                    RoleID.Text = dsUserDetails.Tables[0].Rows[0][0].ToString();
                }
            }
        }
    }
    protected void BtnShowReport_Click(object sender, EventArgs e)
    {  
        //string savePath = Server.MapPath("~") + "upload\\ResultLetter\\" + RegistrationNo.Text + ".pdf";
        string DataSetName = "CandidateWIP";
        // Setup the report viewer object and get the array of bytes
        //ReportViewer viewer = new ReportViewer();
        ReportViewer1.ProcessingMode = ProcessingMode.Local;
        ReportViewer1.LocalReport.ReportPath = Server.MapPath("~") + "\\report\\CandidateWIP.rdlc";
        // ObjectDataSource1.SelectParameters.Add(new Parameter("CandidateId"));
        ReportDataSource datasource = new ReportDataSource(DataSetName, ObjectDataSource1);
        ReportViewer1.LocalReport.DataSources.Clear();
        ObjectDataSource1.DataBind();
        ReportViewer1.LocalReport.DataSources.Add(datasource);
        
    }
    private void GetRegionData()
    {
        ddlRegion.DataSource = GetRegionDataSet();
        ddlRegion.DataTextField = "RegionName";
        ddlRegion.DataValueField = "RegionID";
        ddlRegion.DataBind();
        ddlRegion.Items.Insert(0, new ListItem("-- Select Region --", "0"));
    }
    private DataSet GetRegionDataSet()
    {

        DataSet objDs = null;
        try
        {
            objDs = new RegionMasterBAL().GetRegionMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }
    private void GetCountryData()
    {
        ddlCountry.DataSource = GetCountryDataSet();
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
    }

    private DataSet GetCountryDataSet()
    {
        DataSet objDs = null;
        try
        {
            objDs = new CountryMasterBAL().GetCountryMaster();
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;
    }

    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlRegion.SelectedIndex != 0)
        {   
            GetCountryFromRegionData(Convert.ToInt32(ddlRegion.SelectedValue));
        }
        else
        {
            ddlCountry.Items.Clear();
            ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
        }
    }
    private void GetCountryFromRegionData(int index)
    {
        ddlCountry.DataSource = GetCountryFromRegionDataSet(index);
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("-- Select Country --", "0"));
    }
    private DataSet GetCountryFromRegionDataSet(int regionid)
    {
        DataSet objDs = null;
        try
        {
            objDs = new ActivityMasterBAL().GetCountryFromRegionMaster(regionid);
        }
        catch (Exception exception)
        {
            exception.AddErrorToLog();// throw new ApplicationException(exception.Message);
        }
        return objDs;

    }

}